<template>
  <div>
    <input id="chat-message-input" type="text" size="100"/><br/>
    <input id="chat-message-submit" type="button" value="Send" @click='sendmessage()'/>
  </div>
</template>

<script>
export default {
  name: "socket",
  data() {
    return {}
  },
  methods: {
    sendmessage() {

      window.s.send(document.getElementById("chat-message-input").value);

    }
  },
  mounted() {
    //生成socket对象
    var socket = new WebSocket("ws://127.0.0.1:8000/z/test_socket/");


    socket.onopen = function () {
      console.log('WebSocket open');//成功连接上Websocket
    };
    socket.onmessage = function (e) {
      console.log('message: ' + e.data);//打印服务端返回的数据
    };
    socket.onclose = function (e) {
      console.log(e);
      socket.close(); //关闭TCP连接
    };
    if (socket.readyState == WebSocket.OPEN) {
      socket.onopen();
    }

    window.s = socket;

  }
}
</script>

<style scoped>

</style>
